﻿using GoodShop.Enum;
using GoodShop.seckillRedis;
using GoodShop.Shops;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace GoodShop.seckill
{
    public class SeckillServer : CrudAppService<SeckIll, seckillreadis, Guid, PagedAndSortedResultRequestDto, CreateSeckill>, Iseckill
    {
        private IRepository<CommodityInfo, Guid> repository1;
        public SeckillServer(IRepository<SeckIll, Guid> repository, IRepository<CommodityInfo, Guid> repository2) : base(repository)
        {
            repository1 = repository2;
        }
        [HttpPost("api/app/Addgoods")]
        public async Task<Guid> Addgoods(CreateUpdateShopDTO tj)
        {
            RedisHelper redisHelper = new RedisHelper();
            var ls1 = redisHelper.Get<List<SeckIll>>("yk542345");
            if (ls1 == null)
            {
                ls1 = new List<SeckIll>();
            }
            var shopList = repository1.GetListAsync();
            foreach (var s in await shopList)
            {
                if (s.CommodityId == tj.CommodityId)
                {
                    tj.CommodityId = s.CommodityId;
                    tj.CommodityName = s.CommodityName;
                    tj.CommodityNum = s.CommodityNum;
                    tj.CommoditySubtitle = s.CommoditySubtitle;
                    tj.CommodityWeight = s.CommodityWeight;
                    tj.img1 = s.img1;
                    tj.img2 = s.img2;
                    tj.img3 = s.img3;

                }
                var obj = ObjectMapper.Map<CreateUpdateShopDTO, SeckIll>(tj);
                ls1.Add(obj);

            }

            
           
            redisHelper.Set("yk542345", ls1);
            //redisHelper.Get<List<SeckIll>>("redis53");
            return Guid.Empty ;
        }
        [HttpGet, Route("API/app/Redisget")]

       public List<SeckIll> Redisget()
        {
            RedisHelper redisHelper = new RedisHelper();
            List<SeckIll> ls2 = new List<SeckIll>();
            ls2 = redisHelper.Get<List<SeckIll>>("yk542345");
            int sl = ls2.Count();
            return ls2;
        }
    }

}
